package com.assistant.controller;

import com.assistant.pojo.CommentStar;
import com.assistant.pojo.MainStar;
import com.assistant.service.ItemsPostService;
import com.assistant.service.impl.StarImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/star")
public class StarController {
    @Autowired
    private ItemsPostService itemsPostService;
    @Autowired
    private StarImpl star;


    /*
     *主贴新增 需要前端判断数据合法不为空(update)
     * */
    @PostMapping(value = "/insert/mainstar")
    public ResponseEntity addMainstar(@RequestBody MainStar mainStar) {
        if (star.insertStar(mainStar) == 200) {
            return new ResponseEntity(HttpStatus.OK);
        } else
            return new ResponseEntity(HttpStatus.SERVICE_UNAVAILABLE);
    }

    /*
     * 新增主贴图片
     * */
    @PostMapping("/upload")
    public Integer uploadImgs( @RequestPart("files") MultipartFile[] files) {
        String img = star.upLoadPic(files);
        return star.insertImg(img);
    }

    /*
     * 评论新增 需要前端判断数据合法不为空
     * */
    @PostMapping("/insert/comment")
    public int addComment(@RequestBody CommentStar commentStar) {
        return star.insertComment(commentStar);
    }


    /*
     * 查看所有主贴 只能查看已审核的
     * */
    @GetMapping("/select/mainstar/{status}")
    public List<MainStar> seeMainStar(@PathVariable("status") Integer status) {
        return star.seeMainStar(status);
    }

    /*
     * 查看所有评论 点进主贴看到 按时间升序
     * */
    @GetMapping("/select/commentstar/{id}")
    public List<CommentStar> seeComment(@PathVariable("id") Integer id) {
        return star.seeComment(id);
    }


    /*
     * 删除主贴 连带删除评论
     * */
    @PutMapping("/delete/{id}")
    public int deleteStar(@PathVariable("id") Integer id) {
        return star.deleteMainStar(id);
    }

    /*
     * 查看评论数
     * */
    @GetMapping("/select/commentnum/{id}")
    public int seeCommentNum(@PathVariable("id") Integer id) {
        return star.selectCommentNum(id);
    }

    /*
     * 查看阅读数
     * */
    @GetMapping("/select/seenum/{id}")
    public int getSeeNum(@PathVariable("id") Integer id) {
        return star.selectSeeNum(id);
    }

    /*
     * 点赞新增
     * */
    @PutMapping("/update/like/{id}")
    public ResponseEntity addLike(@PathVariable("id") int id) {
        return star.updateNice(id);
    }

    /*
     * 阅读新增
     * */
    @PutMapping("/update/see/{id}")
    public ResponseEntity addSee(@PathVariable("id") int id) {
        return star.updateSeeNum(id);
    }
    /*
     * 根据主贴t_id返回主贴
     * */
    @GetMapping("/seeMain/{id}")
    public MainStar getMainById(@PathVariable Integer id){
        return star.seeMainStarById(id);
    }
}
